#ifndef ADS1115_H
#define ADS1115_H

#define SDA_PIN GPIO_PIN_7
#define SCL_PIN GPIO_PIN_6
#define RDY_PIN GPIO_PIN11

#define ADS1115_I2Cx I2C1

#define ADDRtoGND 0x90    //接到GND时为0，接到VCC时为1
#define ADDRtoVCC 0x92    //接到GND时为0，接到VCC时为1

#include <stdio.h>
#include "main.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_i2c.h"

extern I2C_HandleTypeDef hi2c;


typedef enum { // register address
  ADS1115_CONVERSION_REGISTER_ADDR = 0,
  ADS1115_CONFIG_REGISTER_ADDR,
  ADS1115_LO_THRESH_REGISTER_ADDR,
  ADS1115_HI_THRESH_REGISTER_ADDR,
} ADS1115_Reg_Addresses_SELECT;

typedef enum { // multiplex options
  ADS1115_MUX_0_1 = 0,
  ADS1115_MUX_0_3,
  ADS1115_MUX_1_3,
  ADS1115_MUX_2_3,
  ADS1115_MUX_0_GND,
  ADS1115_MUX_1_GND,
  ADS1115_MUX_2_GND,
  ADS1115_MUX_3_GND,
} ADS1115_Mux_SELECT;

typedef enum { // full-scale resolution options
  ADS1115_FSR_6_144 = 0,
  ADS1115_FSR_4_096,
  ADS1115_FSR_2_048,
  ADS1115_FSR_1_024,
  ADS1115_FSR_0_512,
  ADS1115_FSR_0_256,
	} ADS1115_FSR_SELECT;



typedef enum {
  ADS1115_MODE_CONTINUOUS = 0,
  ADS1115_MODE_SINGLE
} ADS1115_Mode_SELECT;

typedef enum { // samples per second
  ADS1115_SPS_8 = 0,
  ADS1115_SPS_16,
  ADS1115_SPS_32,
  ADS1115_SPS_64,
  ADS1115_SPS_128,
  ADS1115_SPS_250,
  ADS1115_SPS_475,
  ADS1115_SPS_860
} ADS1115_SPS_SELECT;

//typedef union { // configuration register
//  struct {
//    uint16_t COMP_QUE:2;  // bits 0..  1  Comparator queue and disable
//    uint16_t COMP_LAT:1;  // bit  2       Latching Comparator
//    uint16_t COMP_POL:1;  // bit  3       Comparator Polarity
//    uint16_t COMP_MODE:1; // bit  4       Comparator Mode
//    uint16_t DR:3;        // bits 5..  7  Data rate
//    uint16_t MODE:1;      // bit  8       Device operating mode
//    uint16_t PGA:3;       // bits 9..  11 Programmable gain amplifier configuration
//    uint16_t MUX:3;       // bits 12.. 14 Input multiplexer configuration
//    uint16_t OS:1;        // bit  15      Operational status or single-shot conversion start
//  } bit;
//  uint16_t reg;
//} ADS1115_CONFIG_REGISTER_Type;

typedef enum
{
	RDY_INACTIVE = 0,
	RDY_ACTIVE
}ADS1115_RDY_PIN;

//typedef struct {
//  //ADS1115_CONFIG_REGISTER_Type config;
//  uint8_t address;
//  ADS1115_Reg_Addresses_SELECT last_reg; // save last accessed register
//  //bool changed; // save if a value was changed or not
//} ADS1115_ConfigStruct;

// initialize device
//ads1115_t ads1115_config(uint8_t address); // set up configuration

// set configuration
//void ads1115_set_rdy_pin(ads1115_t* ads, gpio_num_t gpio); // set up data-ready pin
//void ads1115_set_mux(ads1115_t* ads,     ads1115_mux_t mux); // set multiplexer
//void ads1115_set_pga(ads1115_t* ads, ads1115_fsr_t fsr); // set fsr
//void ads1115_set_mode(ads1115_t* ads, ads1115_mode_t mode); // set read mode
//void ads1115_set_sps(ads1115_t* ads, ads1115_sps_t sps); // set sampling speed
void I2C_Init(void);
void ADS1115_I2C_Init(void);

void ADS1115_Reg_Read(I2C_HandleTypeDef *hi2c, uint8_t addr, uint8_t reg, uint16_t* data);
void ADS1115_Reg_Write(I2C_HandleTypeDef *hi2c, uint8_t addr, uint8_t reg, uint16_t* data);

void ADS1115_Set_RDY_PIN(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_RDY_PIN state);
	
void ADS1115_Set_PGA(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_FSR_SELECT fsr);
void ADS1115_Set_MUX(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_Mux_SELECT mux);
void ADS1115_Set_MODE(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_Mode_SELECT mode);
void ADS1115_Set_SPS(I2C_HandleTypeDef* hi2c, uint8_t addr, ADS1115_SPS_SELECT sps);
//int16_t ads1115_get_raw(ads1115_t* ads); // get voltage in bits
//double ADS1115_Get_Voltage(I2C_HandleTypeDef* hi2c, uint8_t addr);
double ADS1115_Get_Voltage(I2C_HandleTypeDef* hi2c, uint8_t addr, float range);

#endif // ifdef ADS1115_H
